package com.cardone.platform.common.service;

import java.util.Date;
import java.util.List;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.lang3.StringUtils;

import com.cardone.common.Characters;
import com.cardone.common.dto.PaginationDto;
import com.cardone.context.ContextHolder;
import com.cardone.platform.common.dao.OperateLogDao;
import com.cardone.platform.common.dto.OperateLogDto;
import com.cardone.platform.configuration.util.DictionaryUtils;

/**
 * 操作日志服务
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class OperateLogDefaultService implements OperateLogService {
	@Override
	public int deleteByIds(final OperateLogDto deleteOperateLog) {
		return ContextHolder.getBean(OperateLogDao.class).deleteByIds(deleteOperateLog);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final OperateLogDto findOperateLog) {
		return ContextHolder.getBean(OperateLogDao.class).findByCodeOrId(mappedClass, findOperateLog);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final OperateLogDto findOperateLog) {
		return ContextHolder.getBean(OperateLogDao.class).findById(mappedClass, findOperateLog);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final OperateLogDto findListOperateLog) {
		return ContextHolder.getBean(OperateLogDao.class).findListByLikeCode(mappedClass, findListOperateLog);
	}

	@Override
	public int insertByCode(final OperateLogDto insertOperateLog) {
		String typeId = DictionaryUtils.readIdByTypeCodeAndCode(insertOperateLog.getTypeTypeCode(), insertOperateLog.getTypeCode());

		if (StringUtils.isBlank(typeId)) {
			typeId = StringUtils.join(insertOperateLog.getTypeTypeCode(), Characters.comma.stringValue(), insertOperateLog.getTypeCode());
		}

		insertOperateLog.setTypeId(typeId);

		return ContextHolder.getBean(OperateLogDao.class).insertByCode(insertOperateLog);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final OperateLogDto paginationOperateLog) {
		return ContextHolder.getBean(OperateLogDao.class).paginationByLikeCode(mappedClass, paginationOperateLog);
	}

	@Override
	public Integer readByCodeNqIdForCount(final OperateLogDto readOperateLog) {
		return ContextHolder.getBean(OperateLogDao.class).readByCodeNqIdForCount(readOperateLog);
	}

	@Override
	public Integer readCountByCodeForToDay(final String typeTypeCode, final String typeCode, final String code) {
		String typeId = DictionaryUtils.readIdByTypeCodeAndCode(typeTypeCode, typeCode);

		if (StringUtils.isBlank(typeId)) {
			typeId = StringUtils.join(typeTypeCode, Characters.comma.stringValue(), typeCode);
		}

		return ContextHolder.getBean(OperateLogDao.class).readCountByCodeForToDay(typeId, code);
	}

	@Override
	public List<Date> readListBeginDateForMax(final String typeTypeCode, final String typeCode, final String code) {
		return this.readListBeginDateForMax(typeTypeCode, typeCode, code, null);
	}

	@Override
	public List<Date> readListBeginDateForMax(final String typeTypeCode, final String typeCode, final String code, final String businessCode) {
		String typeId = DictionaryUtils.readIdByTypeCodeAndCode(typeTypeCode, typeCode);

		if (StringUtils.isBlank(typeId)) {
			typeId = StringUtils.join(typeTypeCode, Characters.comma.stringValue(), typeCode);
		}

		return ContextHolder.getBean(OperateLogDao.class).readListBeginDateForMax(typeId, code, businessCode);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final OperateLogDto saveOperateLog) {
		return ContextHolder.getBean(OperateLogDao.class).saveByIdOrCode(mappedClass, saveOperateLog);
	}

	@Override
	public int updateByCode(final OperateLogDto updateOperateLog) {
		return ContextHolder.getBean(OperateLogDao.class).updateByCode(updateOperateLog);
	}

	@Override
	public int updateEndDateForInvalid(final String typeCode, final String code) {
		return ContextHolder.getBean(OperateLogDao.class).updateEndDateForInvalid(typeCode, code);
	}
}